package com.aaa.web.controller.employeecontroller;

import com.aaa.entity.AppBatteryOrder;
import com.aaa.entity.Per;
import com.aaa.util.OrderNum;
import com.aaa.web.commons.EmpQuery;
import com.aaa.entity.Employee;
import com.aaa.web.commons.Result;
import com.aaa.web.service.iemployeeservice.IEmployeeService;
import com.aaa.web.service.iemployeeservice.IPayService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * PayController
 *
 * @author yyh
 * @date 2018/12/27
 * @time 20:15
 */
@Controller
@RequestMapping("/pay")
@Transactional
public class PayController {
    public static String APPID = "2016092400582812";
    public static String CHARSET = "UTF-8";
    public static String RSA_PRIVATE_KEY = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCbqAijXUbpM1PKKO7dLq4En4E5gix8g0BeEZfvkEy+5vB6uYt9R+TFB12b/2RU5knmA1ZMgVzZLp4l20mwh50fgq2H6H2fQ92d08Vte1QXfQ18Y4H1L6d3NMEvAxeid7UiqbjFc4uy0RiJkFecc8L98pa5djFgZzjM2la2WqTYaGHINTZSdkFHcZV2PdPAOruLlRwT8QVVCKEXFLB7aZRh9Xw9RF2Vc2/v9bHdSRKs0r0EcJTUluzZGgT6Pa5YoDbSFntquu5e2tyCpICxqvR6ofxSU4fZhYFqCMpkIDic5zNyYPnPtKcFDcgBkTuVEp7Rl/fSaTeuEaNFPNiVWCkpAgMBAAECggEBAJMHsUkm5cGH80DFU4PoAQZq6HS7Cmx+ykFRGxR2WJgGqucx9pnbXDPklj6xikNL2EgFpPHVTC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KYq5xFtcHhmJwOJTKHNIfe7FSt0/bDJa+Yezh2weDaY3VwvOfH7XkSa7RaGw/llj08=";
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0uHmUq8w8s+RzUyXD33fZXMVE3muv1e/LT8XAcJ2Sbr8w/liXP4a4juqZwfa4bEgex3O7rhlqCEOuWAmGS8dqmblDf+6cVu9pXj0zy9wi1w+k3Vtn7KwL3tME4V+9FF4GYML3/4cF4dN2UtyfncQzRz2N86AN75D0I/GoGPl4u2Amhdj46ZhWL7twfyudJA4aMmMScDv9ZR6Sm3/lpwmn5sN/Z2N06VXCZd13hnqWE67XcN6jG5kd1QMYP0PAJHpeSfXd7i8GeV8z448H9rWGlXu+V/ScSQxSmWKDI1f0YwSM34Gkr+NyrymBCc4nXn95OCsc5zltO/laic/40BC4wIDAQAB+V9JilLqSa7N7sVqwpvv8zWChgXhX/A96hEg97Oxe6GKUmzaZRNh0cZZ88vpkn5tlgL4mH/dhSr3Ip00kvM4rHq9PwuT4k7z1DpZAf1eghK8Q5BgxL88d0X07m9X96Ijd0yMkXArzD7jg+noqfbztEKoH3kPMRJC2w4ByVdweWUT2PwrlATpZZtYLmtDvUKG/sOkNAIKEMg3Rut1oKWpjyYanzDgS7Cg3awr1KPTl9rHCazk15aNYowmYtVabKwbGVToCAGK+qQ1gT3ELhkGnf3+h53fukNqRH+wIDAQAB";

    @Autowired
    private IPayService payService;

    /**
     *
     * @return
     */
    @RequestMapping("/payresult")
    public String payresult(String phone,String pid) {
        String orderid = OrderNum.createOrderNumber();
        Map map = new HashMap();
        map.put("orderid",orderid);
        map.put("phone",phone);
        payService.addBackOrder(map);
        payService.insertBattery(pid);
        payService.changeBatteryCount(pid);
        return "views/webhtml/employee/payresult";
    }

    /**
     *
     */
    @RequestMapping("/paynotify")
    @ResponseBody
    public Result paynotify() {
        payService.updateState();
        return null;
    }

    @RequestMapping("/payzfb")
    //@CrossOrigin
    public void payment(HttpServletResponse httpResponse, HttpServletRequest httpRequest,String phone,String pid) throws AlipayApiException, IOException {
        /**
         * 订单编号
         */
        Map<String,String> map = new HashMap();
        map.put("money","2");
        map.put("title","共享电瓶订单");
        String out_trade_no = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        //订单总金额  后续可以手动更改
        String total_amount = map.get("money");
        /**
         * 订单标题
         */
        String subject = map.get("title");
        AppBatteryOrder order = new AppBatteryOrder();
        order.setOrderid(out_trade_no);
        order.setOrdername(subject);
        order.setOrdermoney(total_amount);
        payService.addAppOrder(order);
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", APPID, RSA_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://nadirv.natappfree.cc/aaa/pay/payresult?phone="+phone+"&pid="+pid);
        alipayRequest.setNotifyUrl("http://nadirv.natappfree.cc/aaa/pay/paynotify");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+out_trade_no+"\"," +
                "    \"total_amount\":"+total_amount+"," +
                "    \"subject\":\""+subject+"\"," +
                "    \"product_code\":\"QUICK_WAP_WAY\"" +
                "  }");//填充业务参数
        String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        httpResponse.setContentType("text/html;charset=utf-8");
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
    }

}
